package com.ruoyi.teaching.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.teaching.domain.YaStudent;
import com.ruoyi.teaching.domain.YaTeachingPlan;
import com.ruoyi.teaching.enums.DailyTaskEnums;
import com.ruoyi.teaching.mapper.YaStudentMapper;
import com.ruoyi.teaching.mapper.YaTeachingPlanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaDailyTasksMapper;
import com.ruoyi.teaching.domain.YaDailyTasks;
import com.ruoyi.teaching.service.IYaDailyTasksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 每日任务Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-22
 */
@Service
@Slf4j
public class YaDailyTasksServiceImpl extends ServiceImpl<YaDailyTasksMapper,YaDailyTasks> implements IYaDailyTasksService
{
    @Autowired
    private YaDailyTasksMapper yaDailyTasksMapper;
    @Autowired
    private YaTeachingPlanMapper yaTeachingPlanMapper;
    @Autowired
    private YaStudentMapper yaStudentMapper;

    /**
     * 查询每日任务
     *
     * @param id 每日任务主键
     * @return 每日任务
     */
    @Override
    public YaDailyTasks selectYaDailyTasksById(Long id)
    {
        return yaDailyTasksMapper.selectYaDailyTasksById(id);
    }

    /**
     * 查询每日任务列表
     *
     * @param yaDailyTasks 每日任务
     * @return 每日任务
     */
    @Override
    public List<YaDailyTasks> selectYaDailyTasksList(YaDailyTasks yaDailyTasks)
    {
        Long studentID = SecurityUtils.getUserId();
        yaDailyTasks.setStudentId(studentID);
        return yaDailyTasksMapper.selectYaDailyTasksList(yaDailyTasks);
    }

    /**
     * 新增每日任务
     *
     * @param yaDailyTasks 每日任务
     * @return 结果
     */
    @Override
    public int insertYaDailyTasks(YaDailyTasks yaDailyTasks)
    {
        yaDailyTasks.setCreateTime(DateUtils.getNowDate());
        return yaDailyTasksMapper.insertYaDailyTasks(yaDailyTasks);
    }

    /**
     * 修改每日任务
     *
     * @param yaDailyTasks 每日任务
     * @return 结果
     */
    @Override
    public int updateYaDailyTasks(YaDailyTasks yaDailyTasks)
    {
        yaDailyTasks.setUpdateTime(DateUtils.getNowDate());
        return yaDailyTasksMapper.updateYaDailyTasks(yaDailyTasks);
    }

    /**
     * 批量删除每日任务
     *
     * @param ids 需要删除的每日任务主键
     * @return 结果
     */
    @Override
    public int deleteYaDailyTasksByIds(Long[] ids)
    {
        return yaDailyTasksMapper.deleteYaDailyTasksByIds(ids);
    }

    /**
     * 删除每日任务信息
     *
     * @param id 每日任务主键
     * @return 结果
     */
    @Override
    public int deleteYaDailyTasksById(Long id)
    {
        return yaDailyTasksMapper.deleteYaDailyTasksById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean autoCreateDailyTask() {
        try {
            // 1.先获取所有班级当天的课表（这里运用到了mybatis-plus的selectList方法...）
            List<YaTeachingPlan> teachingPlans = yaTeachingPlanMapper.selectList(
                            new LambdaQueryWrapper<YaTeachingPlan>()
                                    // 精确查询根据是否是当天的课程
                                    .eq(YaTeachingPlan::getDate, DateUtils.getDate())
                    );
            // 2.再根据课表自动为每个学员生成每日任务
            // 先遍历当天的课表，在通过课表中的班级id查询对应的学生列表，最后为每个学生生成每日任务
            for (YaTeachingPlan teachingPlan : teachingPlans) {
                // 获取班级id
                Long classesId = teachingPlan.getClassesId();
                List<YaStudent> students = yaStudentMapper.selectList(
                        new LambdaQueryWrapper<YaStudent>()
                                .eq(YaStudent::getClassesId, classesId)
                );
                // 遍历每个学生，为每个学生生成每日任务
                for (YaStudent student : students) {
                    YaDailyTasks dailyTasks = new YaDailyTasks();
                    dailyTasks.setStudentId(student.getId());
                    dailyTasks.setClassesId(classesId);
                    dailyTasks.setStage(teachingPlan.getStage());
                    dailyTasks.setStatus(DailyTaskEnums.UN_START.code);
                    dailyTasks.setDate(teachingPlan.getDate());
                    dailyTasks.setCourseContent(teachingPlan.getCourseContent());
                    dailyTasks.setCreateBy("quartz");
                    dailyTasks.setCreateTime(DateUtils.getNowDate());
                    try {
                        yaDailyTasksMapper.insert(dailyTasks);
                    } catch (Exception e) {
                        log.warn("当前学员已经创建了每日任务{}", dailyTasks);
                    }
                }
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
